﻿namespace BugFixApplication;

// 一个自定义属性 BugFix 被赋给类及其成员
[AttributeUsage(AttributeTargets.Class |
                AttributeTargets.Constructor |
                AttributeTargets.Field |
                AttributeTargets.Method |
                AttributeTargets.Property,
    AllowMultiple = true)]
public class DeBugInfo : Attribute
{
    public string message;

    public DeBugInfo(int bg, string dev, string d)
    {
        BugNo = bg;
        Developer = dev;
        LastReview = d;
    }

    public int BugNo { get; }

    public string Developer { get; }

    public string LastReview { get; }

    public string Message
    {
        get => message;
        set => message = value;
    }
}

[DeBugInfo(45, "Zara Ali", "12/8/2012", Message = "Return type mismatch")]
[DeBugInfo(49, "Nuha Ali", "10/10/2012", Message = "Unused variable")]
internal class Rectangle
{
    // 成员变量
    protected double length;
    protected double width;

    public Rectangle(double l, double w)
    {
        length = l;
        width = w;
    }

    [DeBugInfo(55, "Zara Ali", "19/10/2012",
        Message = "Return type mismatch")]
    public double GetArea()
    {
        return length * width;
    }

    [DeBugInfo(56, "Zara Ali", "19/10/2012",Message = "")]
    public void Display()
    {
        Console.WriteLine("Length: {0}", length);
        Console.WriteLine("Width: {0}", width);
        Console.WriteLine("Area: {0}", GetArea());
    }
} //end class Rectangle  

internal class ExecuteRectangle
{
    public static void TestMain()
    {
        var r = new Rectangle(4.5, 7.5);
        r.Display();
        var type = typeof(Rectangle);
        // 遍历 Rectangle 类的属性
        foreach (var attributes in type.GetCustomAttributes(false))
        {
            var dbi = (DeBugInfo)attributes;
            if (null != dbi)
            {
                Console.WriteLine("Bug no: {0}", dbi.BugNo);
                Console.WriteLine("Developer: {0}", dbi.Developer);
                Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                Console.WriteLine("Remarks: {0}", dbi.Message);
            }
        }
        
        Console.WriteLine("-----------------------");

        // 遍历方法属性
        foreach (var m in type.GetMethods())
        {
            Console.WriteLine("method:"+m.Name);
            if ("GetType" == m.Name)
            {
                continue;
            }
            foreach (Attribute a in m.GetCustomAttributes(true))
            {
                Console.WriteLine("attr:"+a.GetType().Name);
                

                var dbi = (DeBugInfo)a;
                if (null != dbi)
                {
                    Console.WriteLine("Bug no: {0}, for Method: {1}", dbi.BugNo, m.Name);
                    Console.WriteLine("Developer: {0}", dbi.Developer);
                    Console.WriteLine("Last Reviewed: {0}", dbi.LastReview);
                    Console.WriteLine("Remarks: {0}", dbi.Message);
                }
            }
        }

        
        
        Console.WriteLine("end");

        // Console.ReadLine();
    }
}